package com.yuexin.project.store.inbound.controller;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuexin.common.utils.DateUtils;
import com.yuexin.common.utils.poi.ExcelUtil;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.project.store.goods.service.IGoodsListService;
import com.yuexin.project.store.inbound.domain.Inbound;
import com.yuexin.project.store.inbound.service.IInboundService;

/**
 * 入库单 信息操作处理
 * 
 * @author yuexin
 * @date 2018-11-29
 */
@Controller
@RequestMapping("/store/inbound")
public class InboundController extends BaseController
{
    private String prefix = "store/inbound";
	
	@Autowired
	private IInboundService inboundService;
	
	@Autowired
	private IGoodsListService goodsListService;
	
	@RequiresPermissions("store:inbound:view")
	@GetMapping()
	public String inbound()
	{
	    return prefix + "/inbound";
	}
	
	/**
	 * 查询入库单列表
	 */
	@RequiresPermissions("store:inbound:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Inbound inbound)
	{
		startPage();
		inbound.setCompanyId(ShiroUtils.getCompanyId());
        List<Inbound> list = inboundService.selectInboundList(inbound);
		return getDataTable(list);
	}
	
	/**
	 * 新增入库单
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.put("goodsLists", goodsListService.selectGoodsListAll(ShiroUtils.getCompanyId()));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存入库单
	 */
	@RequiresPermissions("store:inbound:add")
	@Log(title = "入库单", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Inbound inbound)
	{		
		inbound.setCompanyId(ShiroUtils.getCompanyId());
		return toAjax(inboundService.insertInbound(inbound));
	}

	/**
	 * 修改入库单
	 */
	@GetMapping("/edit/{inboundId}")
	public String edit(@PathVariable("inboundId") Integer inboundId, ModelMap mmap)
	{
		Inbound inbound = inboundService.selectInboundById(inboundId);
		mmap.put("inbound", inbound);
		mmap.put("goodsLists", goodsListService.selectGoodsListAll(ShiroUtils.getCompanyId()));
	    return prefix + "/edit";
	}
	
	private boolean isThisMonth(Date storeDate) {
		boolean result = false;
		String dateStr = DateUtils.getDate();
		String storeDateStr = DateUtils.dateTime(storeDate);
		String MM=dateStr.substring(5, 7);
		String mm=storeDateStr.substring(5, 7);
		if(MM.equals(mm)) {
			result = true;
		}
		return result;
	}
	
	/**
	 * 修改保存入库单
	 */
	@RequiresPermissions("store:inbound:edit")
	@Log(title = "入库单", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Inbound inbound)
	{		
		Date inboundDate = inbound.getInboundDate();
		if (!isThisMonth(inboundDate))
	    {
	        return error(1, "只能修改当月的入库单");
	    }
		return toAjax(inboundService.updateInbound(inbound));
	}
	
	/**
	 * 删除入库单
	 */
	@RequiresPermissions("store:inbound:remove")
	@Log(title = "入库单", businessType = BusinessType.DELETE)
	@PostMapping( "/remove/{inboundId}")
	@ResponseBody
	public AjaxResult remove(@PathVariable("inboundId") Integer inboundId)
	{		
		Inbound inbound = inboundService.selectInboundById(inboundId);
		Date inboundDate = inbound.getInboundDate();
		if (!isThisMonth(inboundDate))
	    {
	        return error(1, "只能删除当月的入库单");
	    }
		return toAjax(inboundService.deleteInboundById(inboundId));
	}
	
	/**
	 * 导出 
	 */
    @RequiresPermissions("store:inbound:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Inbound inbound)
    {
    	inbound.setCompanyId(ShiroUtils.getCompanyId());
        List<Inbound> list = inboundService.selectInboundList(inbound);
        for(Inbound i : list){
        	Date inboundDate = i.getInboundDate();
        	String inboundDateExcel = DateUtils.dateTime(inboundDate);
        	i.setInboundDateExcel(inboundDateExcel);
        }
        ExcelUtil<Inbound> util = new ExcelUtil<Inbound>(Inbound.class);
        return util.exportExcel(list, "inbound");
    }
	
}
